package com.UBC417.termproject.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.UBC417.termproject.Data.Auction;
import com.UBC417.termproject.Data.Bid;
import com.UBC417.termproject.Data.Buyer;
import com.UBC417.termproject.Data.Item;
import com.UBC417.termproject.Data.User;
import com.google.appengine.api.datastore.EntityNotFoundException;

@SuppressWarnings("serial")
public class RecommendAuctionServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(SubscribeServlet.class
			.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		HttpSession session = req.getSession();
		String buyerId = (String) session.getAttribute("id");

		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();

		List<Auction> recommendations = Buyer.GetRecommendations(buyerId);

		if (recommendations.size() == 0) {
			// no results
			out.println("NO RECOMMENDATIONS YET! Why not bid something first?");
		} else {
			resp.getWriter().println("<table border='1'>");
			out.println("<tr>");

			out.println("<td>Item Name</td>");
			out.println("<td>Seller</td>");
			out.println("<td>Base Price</td>");
			out.println("<td>Start Date</td>");
			out.println("<td>End Date</td>");
			out.println("<td>Status</td>");
			out.println("<td>Highest Bid</td>");
			//out.println("<td>Auction ID</td>");
			out.println("<td>Bid</td>");
			out.println("</tr>");
			for (Auction a : recommendations) {
				Bid b = User.getHighestBid(a.getId());
				Item i = User.getAuctionItem(a.getId());
				out.println("<tr>");
				out.println("<td>" + a.getName() + "</td>");
				out.println("<td><a>" + a.getUserId() + "</a></td>");
				out.println("<td>" + i.getBasePrice() + "</td>");
				out.println("<td>" + a.getStartTime() + "</td>");
				out.println("<td>" + a.getEndTime() + "</td>");
				out.println("<td>" + a.getStatus() + "</td>");
				out.println("<td>" + b.getPrice() + "</td>");
				//out.println("<td>" + a.getId() + "</td>");
				out.println("<td><input type='button' id= '"
						+ a.getId()
						+ "' data-bp='"
						+ i.getBasePrice()
						+ "' data-hp='"
						+ b.getPrice()
						+ "' onclick= 'bidItem(this)' value='Bid Now'/input></td>");
				out.println("</tr>");
			}
			out.println("</table>");

		}
	}

}
